//
//  SelectRepairNextRegionViewController.m
//  customer
//
//  Created by Holothurian on 2018/11/4.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "SelectRepairNextRegionViewController.h"
#import "CasesShowCollectionViewCell.h"
#import "SelectRepairNextRegionViewController.h"
#import "SelectRepairRegionDetailViewController.h"
#import "OneKeyRepairViewController.h"

static NSString *ItemIdentifier = @"ItemIdentifier";
@interface SelectRepairNextRegionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>{
    UICollectionViewFlowLayout *_flowLayout;
}
@property (nonatomic,strong)NSMutableArray *dataSource;

@property (nonatomic,strong)UICollectionView *collectionView;

@end

@implementation SelectRepairNextRegionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self layoutCollectionView];
    [self loadData];
}

- (void)layoutCollectionView{
    self.title = self.repairModel.parts_name?:@"维修区域";
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _flowLayout.itemSize = CGSizeMake((F_DEVICE_W - 45) / 2.0, 140);
    _flowLayout.minimumLineSpacing = 20;
    _flowLayout.minimumInteritemSpacing = 15;
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, F_DEVICE_W, F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0)) collectionViewLayout: _flowLayout];
    collectionView.bounces = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    self.collectionView = collectionView;
    [self.collectionView registerClass:[CasesShowCollectionViewCell class] forCellWithReuseIdentifier:ItemIdentifier];
    
    
}

- (void)loadData{
    [self showLoading];
    AutoWeakSelf
    [[QYNetManager sharedManager]POST:kGetMaintailPartss parameters:@{@"parent_id":self.repairModel.parts_id?:@""} andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [weakSelf hideLoading];
        if (self.dataSource.count) {
            [self.dataSource removeAllObjects];
        }
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [weakSelf showErrorTipWithTitle:responseObject[@"error"]];
        }else{
            NSArray *dataSource = responseObject[@"data"];
            [weakSelf.dataSource addObjectsFromArray:[NSArray yy_modelArrayWithClass:[SelectRepairModel class] json:dataSource]];
            [weakSelf.collectionView reloadData];
        }
        
    } Failure:^(NSError *error) {
        [weakSelf hideLoading];
        [weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.dataSource count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CasesShowCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ItemIdentifier forIndexPath:indexPath];
    SelectRepairModel *model = self.dataSource[indexPath.row];
    [cell cellForRepairLayout:model];
    return cell;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SelectRepairModel *subModel = self.dataSource[indexPath.row];
    if ([subModel.is_hava integerValue]) {
        SelectRepairRegionDetailViewController *detail = [[SelectRepairRegionDetailViewController alloc]init];
        detail.fatherRepairModel = self.repairModel;
        detail.subRepairModel = subModel;
        detail.title = subModel.parts_name;
        TAB_PUSH(self, detail, YES);
    }else{
        NSArray *subViews = self.navigationController.viewControllers;
        for (BaseViewController *baseVC in subViews) {
            if ([baseVC isKindOfClass:[OneKeyRepairViewController class]]) {
                baseVC.isPop = YES;
                baseVC.popArray = @[self.repairModel,subModel];
                [self.navigationController popToViewController:baseVC animated:YES];
            }
        }
    }
    
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
